const Promise = require('bluebird'); //
const JiraTool = require('../Tools/JiraTool');
const Pool = require('../Tools/Pool').Pool;
const PoolJira=require('../Tools/Pool').PoolJira;
const TimeTool = require('../Tools/TimeTool');
const JiraDBPool = require('../Tools/JiraPool');
const JiraDbModel = require('./jiraDbModel'); 

const Project = require('./Project');
const TestRecord = require('./TestRecord');
const TestReport = require('./TestReport');

const _ = require('lodash');
const {jira,user} = require('../Configs');


let request = require('request');
request = Promise.promisifyAll(request); //

const {
  JIRA_HOST
} = jira;

/**
 * Jira相关请求和数据库操作
 */
class JiraModels {
  

  async getRCV(pname,version){
    const ReturnNumber = '%e5%9b%9e%e5%bd%92%e6%ac%a1%e6%95%b0'
    const pkey = await JiraDbModel.jiraDbPkey(pname)
    version =encodeURIComponent(version)
    let sum =0;
    for (let j = 1; j <= 15; j++) {

      let api = `${JIRA_HOST}/rest/api/2/search?jql=project = ${pkey} AND affectedVersion = '${version}' 
      AND ${ ReturnNumber}<= ${j} AND ${ReturnNumber} >= ${j}`;

      let res = await request.getAsync(api, user)
      let ret = JSON.parse(res.body)
      let total = ret.total * j
      sum += total
    }
    return sum;
  }
  
  async getReopenCountByVersion(pname,version) {
    const reopenCount = 'reopen%e6%ac%a1%e6%95%b0'
    const pkey =await JiraDbModel.jiraDbPkey(pname)
    version =encodeURIComponent(version)
    let sum=0;

    for (let j = 1; j <= 15; j++) {
      let api = `${JIRA_HOST}/rest/api/2/search?jql=project = ${pkey} AND affectedVersion = '${version}' 
      AND  ${reopenCount} <= ${j}  AND  ${reopenCount} >= ${j}`

      let res = await request.getAsync(api, user)
      let ret = JSON.parse(res.body)
      let total = ret.total * j
      sum += total
     }
    return sum;
  }

}



// const bL = new JiraModel();
// let res = await bL.getRCV('Education','0.8.5')
// console.log('11..',res)
module.exports = new JiraModels();